package com.linghu.ai.repository;

import com.linghu.ai.entity.Device;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 设备Repository接口
 */
@Repository
public interface DeviceRepository extends JpaRepository<Device, Long> {
    
    /**
     * 根据MAC地址查找设备
     */
    Optional<Device> findByMacAddress(String macAddress);
    
    /**
     * 根据IP地址查找设备
     */
    Optional<Device> findByIpAddress(String ipAddress);
    
    /**
     * 根据状态查找设备
     */
    List<Device> findByStatus(Device.DeviceStatus status);
    
    /**
     * 查找在线设备
     */
    @Query("SELECT d FROM Device d WHERE d.status = 'ONLINE'")
    List<Device> findOnlineDevices();
    
    /**
     * 根据设备类型查找
     */
    List<Device> findByDeviceType(String deviceType);
    
    /**
     * 检查MAC地址是否存在
     */
    boolean existsByMacAddress(String macAddress);
    
    /**
     * 根据MAC地址更新设备状态
     */
    @Query("UPDATE Device d SET d.status = :status, d.lastOnlineTime = CURRENT_TIMESTAMP WHERE d.macAddress = :macAddress")
    void updateDeviceStatusByMacAddress(@Param("macAddress") String macAddress, @Param("status") Device.DeviceStatus status);
} 